package com.fake

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.viewModels
import com.bumptech.glide.Glide
import com.helloandroid.AppUtil
import com.helloandroid.R
import com.helloandroid.WodeViewModel
import com.helloandroid.activitys.BodyInfoActivity
import com.helloandroid.activitys.BsToolActivity
import com.helloandroid.activitys.BuShuRecordActivity
import com.helloandroid.activitys.SettingActivity
import com.helloandroid.base.BaseFragment
import com.helloandroid.dialogs.BsTargetDialog
import com.helloandroid.tools.AppConfig
import com.tencent.mm.opensdk.modelmsg.SendAuth

class FakeWodeFragment : BaseFragment() {

    lateinit var tvTarget: TextView
    lateinit var tvName: TextView
    lateinit var btnBindWx: Button
    lateinit var ivUserIcon: ImageView

    lateinit var tvCoin: TextView
    lateinit var bindWxReceiver: FakeBindWxReceiver
    val model: WodeViewModel by viewModels()

    override fun getLayoutResId(): Int = R.layout.fragment_fake_wode

    override fun initView(view: View) {
        tvTarget = findViewById(R.id.tvTarget)
        tvName = findViewById(R.id.tvName)
        ivUserIcon = findViewById(R.id.ivUserIcon)
        btnBindWx = findViewById(R.id.btnBindWx)
        tvCoin = findViewById(R.id.tvCoin)

        // 步数记录
        findViewById<LinearLayout>(R.id.llBuShuRecord).setOnClickListener {
            startActivity(Intent(activity, BuShuRecordActivity::class.java))
        }

        // 身体报告
        findViewById<LinearLayout>(R.id.llBodyInfo).setOnClickListener {
            startActivity(Intent(activity, BodyInfoActivity::class.java))
        }

        // 步数小工具
        findViewById<LinearLayout>(R.id.llTools).setOnClickListener {
            startActivity(Intent(activity, BsToolActivity::class.java))
        }

        // 每日目标
        findViewById<LinearLayout>(R.id.llDailyTarget).setOnClickListener {
            activity?.apply {
                val dialog = BsTargetDialog(this)
                dialog.show()
            }
        }

        // 设置
        findViewById<LinearLayout>(R.id.llSetting).setOnClickListener {
            startActivity(Intent(activity, SettingActivity::class.java))
        }

        // 绑定微信
        findBtn(R.id.btnBindWx).setOnClickListener {
            bindWx()
        }

        updateUI()
    }

    override fun initData() {
        bindWxReceiver = FakeBindWxReceiver()
        requireActivity().registerReceiver(bindWxReceiver, IntentFilter(AppConfig.Action_User_BindWx))

        model.perSecondTick(this::perSecondTick)
    }

    private fun perSecondTick(){
        tvTarget.text = AppUtil.user.bs_target.toString()

        // 更新 金币或 money
        updateCoinAndMoney()
    }

    fun updateUI() {
        if (AppUtil.user.wx_id != "") {
            tvName.text = "${AppUtil.user.wx_name}"
            btnBindWx.visibility = View.GONE
            Glide.with(this)
                    .load(AppUtil.user.wx_icon)
                    .centerCrop()
                    .placeholder(R.drawable.dd_user_icon_default)
                    .into(ivUserIcon)

        } else {
            tvName.text = AppUtil.getWelComeName()
            btnBindWx.visibility = View.VISIBLE
            ivUserIcon.setImageResource(R.drawable.dd_user_icon_default)
        }

        // 更新当前金币信息
        updateCoinAndMoney()

        // 更新当前步数信息
        tvTarget.text = AppUtil.user.bs_target.toString()
    }
    private fun updateCoinAndMoney() {
        val user = AppUtil.user
        tvCoin.text = "${user.coin}"
    }
    override fun onDestroyView() {
        requireActivity().unregisterReceiver(bindWxReceiver)
        super.onDestroyView()
    }
    private fun bindWx() {
        if (!AppUtil.wxApi.isWXAppInstalled) {
            AppUtil.toast("您还未安装微信客户端!")
            return
        }
        val seq = SendAuth.Req()
        seq.scope = "snsapi_userinfo"
        seq.state = "ttjb_wx_login"
        AppUtil.wxApi.sendReq(seq)
    }

    inner class FakeBindWxReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null && intent.action == AppConfig.Action_User_BindWx) {
                // 更新UI,更新玩家信息
                updateUI()
            }
        }
    }
}